﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using JustinAngel.Neurons.Framework;
using JustinAngel.Neurons.Framework.Helpers;
using JustinAngel.Neurons.Framework.Messages;
using JustinAngel.Neurons.Framework.Navigation;
using JustinAngel.UnitedNationsNews.Framework;
using JustinAngel.UnitedNationsNews.Pages.Photos;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.SilverlightMediaFramework.Plugins.Primitives;
using Ninject;

namespace JustinAngel.UnitedNationsNews.Pages.Video
{
    public partial class VideoPage : BasePage
    {
        public VideoPage()
        {
            InitializeComponent();

            IoC.Get<IMessenger>().Register<MediaBarVisiblityChangedMessage>(this,
                msg => DispatcherTimerHelper.On(TimeSpan.FromMilliseconds(200),
                    () => OnPhotosDetailsPaneVisiblityChanged(msg)));

            IoC.Get<IMessenger>().Register<SetVideoPlayerCurrentPlaybackPositionMessage>(this,
                msg => smfPlayer.SeekToPosition(msg.Content));

            IsolatedStorageHelper = IoC.Get<IIsolatedStorageHelper>();

            IoC.Get<IMessenger>().Register<ApplicationDeactivatedMessage>(this, OnAppDecativatedStoreCurrentPlaybackPosition);
        }

        private void OnAppDecativatedStoreCurrentPlaybackPosition(ApplicationDeactivatedMessage obj)
        {
            PhoneApplicationService.Current.State["VideoAndRadioViewModel.CurrentPlaybackPosition"] =
                SerializationHelper.ToJsonString(IsolatedStorageHelper.GetSetting("LastPlaybackPositionTotalSeconds"));
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            IsolatedStorageHelper.SetSetting("LastPlaybackPositionTotalSeconds", smfPlayer.PlaybackPosition.TotalSeconds);

            base.OnNavigatingFrom(e);
        }

        private IApplicationBar _cachedAppBar = null;
        private void OnPhotosDetailsPaneVisiblityChanged(MediaBarVisiblityChangedMessage obj)
        {
            if (obj.Content)
            {
                this.ApplicationBar = _cachedAppBar;
            }
            else
            {
                _cachedAppBar = this.ApplicationBar;
                this.ApplicationBar = null;
            }

        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            AddNavigatingText().Margin = new Thickness(32, 0, 0, 0);

            IsolatedStorageHelper.SetSetting("LastPlaybackPositionTotalSeconds", smfPlayer.PlaybackPosition.TotalSeconds);

            DoBaseBackKey(e);
        }

        [Inject]
        public IIsolatedStorageHelper IsolatedStorageHelper { get; set; }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled;
        }
    }
}